const submitBtn = document.getElementById('submit')//提交按钮
const usernameIpt = document.getElementById('username')//用户名输入框
const pwdIpt = document.getElementById('pwd')
const iptList = ["username","pwd","repPwd"]

// let isPass = false
// let isPwdPass = false
// let valiRes = {
//     username:false,
//     pwd:false,
//     repPwd:false
// }
let valiRes = genValiRes(iptList)
/*
usernameIpt.onchange = function(){
    const value = this.value
   
    if(value.length <= 3){
        console.log("验证不通过")
        valiRes.username = false
    }else{
        console.log("验证通过")
        valiRes.username = true
    }
    console.log(value)
    
}
pwdIpt.onchange = function(){
    const value = this.value
    if(value.length <= 3){
        console.log("密码验证不通过")
        valiRes.pwd = false
    }else{
        console.log("密码验证通过")
        valiRes.pwd = true
    }
    console.log(value)
    
}

submitBtn.onclick = function(e){
     //判断用户名格式是否正确
    if(valiRes.username && valiRes.pwd ){
        console.log('通过了')
    }else{
        e.preventDefault()
        console.log('没通过了')
    }
    console.log('点解了')
}
*/
iptList.forEach(function(id){
    const element = document.getElementById(id)
    element.onchange = function(){
        const value = this.value
        if(value.length <= 3){
            console.log("密码验证不通过")
            valiRes[id] = false
        }else{
            console.log("密码验证通过")
            valiRes[id] = true
        }
        console.log(value)
        
    }
})

submitBtn.onclick = function(e){
    //判断表单是否全部通过
    const isFormPass =  computeValiRes(valiRes)
    console.log(isFormPass)
   if(isFormPass){
       console.log('通过了')
   }else{
       e.preventDefault()
       console.log('没通过了')
   }
   console.log('点解了')
}
/**
 * 根据iptList 字段的id列表创建一个表单验证结果
 * @param {Array} idList 
 */
function genValiRes(idList){
    const valiRes = {}
    idList.forEach(function(id){
        valiRes[id] = false
    })
    return valiRes
}


/**
 * 验证结果对象，验证是否全部为true，也就是也在所有输入框是否全部通过
 * 如果有一个输入框验证结果为false则不通过返回false
 * 如果所有输入框验证结果为true则通过返回true
 * @param {Object} idList 
 */
function computeValiRes(valiRes){
    for(let key in valiRes){
        const value = valiRes.key //fals
        if(!value){
            return false
        }
    }
    return true
}
computeValiRes(valiRes)